//
//  ToolsServer.m
//  Base_App
//
//  Created by Yutian Duan on 2019/5/30.
//  Copyright © 2019年 Wanwin. All rights reserved.
//

#import "ToolsServer.h"

@implementation ToolsServer

+ (instancetype)sharedInstance {
  static ToolsServer *instance = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    instance = [[ToolsServer alloc] init];
  });
  return instance;
}

- (instancetype)init {
  self = [super init];
  if (self) {
//    _isLoginApp = YES;
//    _isBindCard = YES;
  }
  return self;
}


- (void)loginWithAccount:(NSString *)account password:(NSString *)password finished:(BaseServer)finish failed:(BaseServerError)failed {
  
  NSDictionary *params = @{
                           @"mobile":account,
                           @"password":password
                           
                           };
  
  [self startHTTPWithMethod:YFRequestMethodGET params:params key:@"login/doApplogin" finished:^(Result *returnValue) {
    if (returnValue.status == 200) {
      self.userModel = [UserInfoModel yy_modelWithJSON:returnValue.data];
      ////! 记住账号密码
      [AppUser setPhone:account password:password];
      self.isLoginApp = YES;
      self.isBindCard = self.userModel.islock;
      finish(YES);
    } else {
      finish(NO);
    }
    
  } failed:^(id errorCode) {
    failed(errorCode);
  }];

}



///! 注册
- (void)registAppWithAccount:(NSString *)account
                        code:(NSString *)code
                    password:(NSString *)password
                   againword:(NSString *)againword
                    finished:(BaseServer)finish
                      failed:(BaseServerError)failed {
  
  NSDictionary *parmas = @{
                           @"phone":account,
                           @"phonecode":code,
                           @"password":password,
                           @"password1":againword
                           };
  
  [self startHTTPWithMethod:YFRequestMethodGET params:parmas key:@"login/doAppreg" finished:^(Result *returnValue) {
    if (returnValue.status == 200) {
      ///! 保存手机号 和 密码
      [AppUser setPhone:password];
      finish(YES);
    } else {
      finish(NO);
    }
    
  } failed:^(id errorCode) {
    
  }];
  
  
}

///! 发验证码
- (void)sendmassgeCodeWithPhone:(NSString *)phone finished:(BaseServer)finish failed:(BaseServerError)failed {
  
  NSDictionary *parmas = @{
                           @"phone":phone
                           };
  
  [self startHTTPWithMethod:YFRequestMethodGET params:parmas key:@"sms/getuserregcode" finished:^(Result *returnValue) {
    if (returnValue.status == 200) {
      finish(YES);
    } else {
      [Tools showErrorWithStatus:returnValue.msg];
      finish(NO);
    }
    
  } failed:^(id errorCode) {
    failed(errorCode);
  }];
  
  
}

///! 退出登录
- (void)loginOut {
  self.isLoginApp = NO;
  self.isBindCard = NO;
  self.userModel = nil;
  [AppUser loginOut];
}


@end
